%calcMW will calculate the summed hydropathy index of an amino acid
%sequence.
%
%  HPI = calcHPI(Seq)
%
%  Info = calcHPI
%
%  INPUT
%    Seq: string or cell of string of amino acid sequences
%         *If empty, will return the Info structure
%
%  OUTPUT
%    HPI: value or matrix of HPI values
%    Info: structure of HPI data
%
%  REFERENCE
%  A simple method for displaying the hydropathic character of a protein.
%  Kyte J, Doolittle RF. J Mol Biol. 1982 May 5;157(1):105-32.

function HPI = calcHPI(Seq)
Info = {...
    'A',  1.8000; ...
    'R', -4.5000; ...
    'N', -3.5000; ...
    'D', -3.5000; ...
    'C',  2.5000; ...
    'Q', -3.5000; ...
    'E', -3.5000; ...
    'G', -0.4000; ...
    'H', -3.2000; ...
    'I',  4.5000; ...
    'L',  3.8000; ...
    'K', -3.9000; ...
    'M',  1.9000; ...
    'F',  2.8000; ...
    'P', -1.6000; ...
    'S', -0.8000; ...
    'T', -0.7000; ...
    'W', -0.9000; ...
    'Y', -1.3000; ...
    'V',  4.2000; ...
    };
if nargin == 0
    HPI = Info;
    return
end

AAHPI = cell2mat(Info(:, 2)); %amino acid mw

if iscell(Seq)
    HPI = zeros(size(Seq));
    for j = 1:numel(Seq)
        HPI(j) = sum(AAHPI .* countAA(Seq{j}, Info));
    end
else
    HPI = sum(AAHPI .* countAA(Seq, Info));
end

function C = countAA(Seq, Info)
Seq = upper(Seq);
C = cellfun(@(x) sum(Seq == x), Info(:, 1));